package io.renren.modules.demo.controller;

import cn.hutool.core.util.ObjectUtil;
import io.renren.common.annotation.LogOperation;
import io.renren.common.constant.Constant;
import io.renren.common.page.PageData;
import io.renren.common.utils.ConvertUtils;
import io.renren.common.utils.Result;
import io.renren.modules.demo.dto.XlsUsersDrawsDTO;
import io.renren.modules.demo.service.XlsUsersDrawsService;
import io.renren.modules.security.entity.SysUserTokenEntity;
import io.renren.modules.security.service.ShiroService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Map;


/**
 * 用户提现记录
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2020-04-16
 */
@RestController
@RequestMapping("demo/xlsusersdraws")
@Api(tags = "用户提现记录")
public class XlsUsersDrawsController {

    private static Logger logger = LoggerFactory.getLogger(XlsUsersDrawsController.class);

    @Autowired
    private XlsUsersDrawsService xlsUsersDrawsService;
    @Autowired
    private ShiroService shiroService;

    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType = "String")
    })
    @RequiresPermissions("demo:xlsusersdraws:page")
    public Result<PageData<XlsUsersDrawsDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params) {
        PageData<XlsUsersDrawsDTO> page = xlsUsersDrawsService.queryPage(params);

        return new Result<PageData<XlsUsersDrawsDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    @RequiresPermissions("demo:xlsusersdraws:info")
    public Result<XlsUsersDrawsDTO> get(@PathVariable("id") Long id) {
        XlsUsersDrawsDTO data = xlsUsersDrawsService.selectXlsUsersDrawsById(id);

        return new Result<XlsUsersDrawsDTO>().ok(data);
    }

    @PutMapping("/auditPass")
    @ApiOperation("审核通过")
    @LogOperation("审核通过")
    public Result auditPass(@RequestBody XlsUsersDrawsDTO dto,@RequestHeader("token") String token) {
        SysUserTokenEntity byToken = shiroService.getByToken(token);
        if (ObjectUtil.isEmpty(byToken)) {
            return new Result().error("用户未登录，请先登录！");
        }
        try {
            xlsUsersDrawsService.updataAuditPass(byToken, dto, true);
        } catch (Exception e) {
            logger.error("审核通过操作失败 -> {}", e.getMessage());
            return new Result().error("审核通过处理失败！");
        }

        return new Result();
    }

    @PutMapping("/auditRefused")
    @ApiOperation("审核拒绝")
    @LogOperation("审核拒绝")
    public Result auditRefused(@RequestBody XlsUsersDrawsDTO dto,@RequestHeader("token") String token) {
        SysUserTokenEntity byToken = shiroService.getByToken(token);
        if (ObjectUtil.isEmpty(byToken)) {
            return new Result().error("用户未登录，请先登录！");
        }
        try {
            xlsUsersDrawsService.updataAuditRefused(byToken, dto, false);
        } catch (Exception e) {
            logger.error("审核拒绝操作失败 -> {}", e.getMessage());
            return new Result().error("审核拒绝操作失败！");
        }
        return new Result();
    }

}